import Vue from "vue";
import Vuex from "vuex";
import Cookie from "@u/cookie";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    menuType: false,
    menuList: [],
    token: "",
    permissions: "",
    userInfo: ""
  },
  mutations: {
    //  设置 token
    setToken(state, token) {
      state.token = token;
      Cookie.set("token", token);
    },
    setPermissions(state, permissions) {
      state.permissions = permissions;
      localStorage.setItem("mb_permissions", JSON.stringify(permissions));
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo;
      Cookie.set("mb_userInfo", JSON.stringify(userInfo));
      localStorage.setItem("mb_userInfo", JSON.stringify(userInfo));
    },
    setMenuType(state, data) {
      window.console.log(data);
      state.menuType = data;
    },
    setMenu(state, data) {
      state.menuList = data;
    }
  },
  actions: {
    //  设置 token
    setToken({ commit }, token) {
      commit("setToken", token);
    },
    setPermissions({ commit }, permissions) {
      commit("setPermissions", permissions);
    },
    setUserInfo({ commit }, userInfo) {
      commit("setUserInfo", userInfo);
    },
    setMenuType({ commit }, data) {
      commit("setMenuType", data);
    },
    setMenu({ commit }, data) {
      commit("setMenu", data);
    }
  }
});
